package app_dao

import (
	"fmt"
	"hft/Database/admin"
	"hft/Http/Services/front/pc/app_model"
	"hft/global"
	"strings"
	"time"

	"gorm.io/gorm"
)

type Index struct{}

// 是否需要加载tags
func (i *Index) IsPreloadTags(db *gorm.DB, isPreload bool) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if isPreload {
			return db.Preload("Tags")
		} else {
			return db
		}
	}
}

func (i *Index) GetTeSeLou(db *gorm.DB, teSeName string, regionLink string, isPreload bool, isAddress bool) (list []*app_model.Lou) {
	var lous []*admin.LouPan
	db.Scopes(i.IsPreloadTags(db, isPreload)).Joins("LEFT JOIN hft_loupan_tese ON `hft_loupans`.`id` = `hft_loupan_tese`.`lou_pan_id`").Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Where(fmt.Sprintf("`hft_loupan_tese`.`lou_pan_te_se_name` = \"%v\"", teSeName)).Order("sort desc").Limit(20).Find(&lous)
	var louList = make([]*app_model.Lou, 0)
	for k, v := range lous {
		var belongStr = strings.Split(v.BelongName, "-")[2]
		var cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
		var tags = []string{}
		var areaRange = ""
		var address = ""
		if k%5 == 0 {
			for _, j := range v.Tags {
				tags = append(tags, j.Name)
			}
			if v.MaxArea != 0 && v.MinArea != 0 {
				areaRange = fmt.Sprintf("建面 %v-%vm²", v.MinArea, v.MinArea)
			}
			if isAddress {
				address = belongStr + "-" + v.Address
			}
		}

		louList = append(louList, &app_model.Lou{ID: v.ID, Name: v.Name, Price: v.ReferencePrice, Region: belongStr, Status: v.Status, OpenTime: "", Cover: cover, Tag: tags, AreaRange: areaRange, Address: address})
	}
	return louList
}

func (*Index) GetOpenTime(db *gorm.DB, regionLink string) (list []*app_model.Lou) {
	var lous []*admin.LouPan
	db.Preload("SellLouPanInfo").Joins("LEFT JOIN hft_sell_lou_pan_info ON `hft_loupans`.`id`= `hft_sell_lou_pan_info`.`lou_pan_id`").Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Order("`hft_sell_lou_pan_info`.`open_time` desc").Limit(20).Find(&lous)
	var louList = make([]*app_model.Lou, 0)
	for _, v := range lous {
		var belongStr = strings.Split(v.BelongName, "-")[2]
		var cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
		t := time.Unix(int64(v.SellLouPanInfo.OpenTime/1000), 0)
		var openTime = ""
		if v.SellLouPanInfo.OpenTime != 0 {
			openTime = t.Format("2006-01-02")
		}
		louList = append(louList, &app_model.Lou{ID: v.ID, Name: v.Name, Price: v.ReferencePrice, Region: belongStr, Status: v.Status, OpenTime: openTime, Cover: cover, Tag: []string{}})
	}
	return louList
}

func (*Index) GetLou(db *gorm.DB, regionLink string) (list []*app_model.Lou) {
	var lous []*admin.LouPan
	db.Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Order("create_time desc").Limit(20).Find(&lous)
	var louList = make([]*app_model.Lou, 0)
	for _, v := range lous {
		var belongStr = strings.Split(v.BelongName, "-")[2]
		var cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
		var create_time = v.CreateTime.Format("2006年01月02日")
		louList = append(louList, &app_model.Lou{
			ID: v.ID, Name: v.Name,
			Price:      v.ReferencePrice,
			Region:     belongStr,
			Status:     v.Status,
			OpenTime:   "",
			Cover:      cover,
			Tag:        []string{},
			CreateTime: create_time,
		})
	}
	return louList
}

func (*Index) GetArt(db *gorm.DB, regionLink string, cate []int, needCont bool) (list []*app_model.Article) {
	var art []*admin.ArticleBase
	db.Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Where("art_type IN ?", cate).Order("create_time desc").Limit(20).Find(&art)
	var artList = make([]*app_model.Article, 0)
	for _, v := range art {
		var cover = global.OC_CONFIG.AliyunOSS.BucketUrl + v.Cover
		var createTime = v.CreateTime.Format("2006-01-02")
		artList = append(artList, &app_model.Article{ID: v.ID, Name: v.Name, Cover: cover, Author: v.Author, CreateTime: createTime, HasVideo: v.HasVideo, Cont: v.PureText})
	}
	return artList
}

func (*Index) GetAct(db *gorm.DB, regionLink string) (list []*app_model.ActiveItem) {
	var ActiveList []*admin.ActiveBase
	db.Where("region_link LIKE ?", fmt.Sprintf("%v%%", regionLink)).Order("create_time desc").Limit(20).Find(&ActiveList)
	var actives = make([]*app_model.ActiveItem, 0)
	for _, v := range ActiveList {
		actives = append(actives, &app_model.ActiveItem{ID: v.ID, Name: v.Name, LouPanID: uint(v.LouPanId)})
	}
	return actives
}

var PCIndexDao = new(Index)
